<?php
namespace App\Admin\Controllers;

use App\Admin\Models\Config;
use Illuminate\Http\Request;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\Storage;
use function foo\func;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\DB;
class ConfigController extends AdminController
{
    protected $title = '网站配置';


    public function index(Content $content)
    {
        $form = new Form(new Config);
        $form->setTitle('网站设置');
        $form->setAction('indexSave');
        $form->tools(function (Form\Tools $tools) {
            $tools->disableList();
        });

        $this->config = Config::all();
        $form->tab('风控设置', function ($form) {
            foreach ($this->config as $config){
                if($config->group == '1'){
                    if($config->note == ''){
                        $form->{$config->type}('row['.$config->name.']',$config->key)->value($config->value);
                    }else{
                        $form->{$config->type}('row['.$config->name.']',$config->key)->value($config->value)->setWidth(5)->help($config->note);
                    }
                }
            }
        });

//        $form->tab('推广背景图', function ($form) {
//            foreach ($this->config as $config) {
//                if ($config->group == '4') {
//                    $form->{$config->type}('row['.$config->name.']',$config->key)->move($config->value,'50px','50px');
//                }
//            }
//        });
        $form->tab('提现说明', function ($form) {
            foreach ($this->config as $config) {
                if ($config->group == '2') {
                    $form->{$config->type}($config->name,$config->key)->value($config->value);
                }
            }
        });
        $form->tab('入网协议', function ($form) {
            foreach ($this->config as $config) {
                if ($config->group == '3') {
                    $form->{$config->type}($config->name,$config->key)->value($config->value);
                }
            }
        });

        $form->disableCreatingCheck();
        $form->disableEditingCheck();
        $form->disableViewCheck();
        $form->disableReset();

        return $content->description('此页配置您的系统参数')
            ->body($form);
    }
    public function indexSave(Request $request)
    {
        $all = $request->post('row');
        if($request->post('ruwang')){
            $all['ruwang'] = $request->post('ruwang');
        }
        if($request->post('earnings')){
            $all['earnings'] = $request->post('earnings');
        }
        $all['background_img'] = $request->post('background_img');
        foreach ($all as $nam => $val){
            DB::table('shop_setting')->where('name',$nam)->update(['value'=>$val]);
        }

        admin_success('保存成功！');

        return back();
    }

}
